package com.xm.controller.web;

import com.xm.pojo.Device;
import com.xm.pojo.FlowDailyReport;
import com.xm.pojo.FlowHistory;
import com.xm.pojo.Sysuser;
import com.xm.service.DeviceService;
import com.xm.service.FlowDailyReportService;
import com.xm.service.FlowHistoryService;
import com.xm.util.JSONResult;
import com.xm.util.excel.ExcelUtils;
import com.xm.util.page.PageUtils;
import com.xm.util.print.PrintUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by wzl on 2018/4/9.
 */
@Controller
@RequestMapping("/web/flowDailyReport")
public class FlowDailyReportController {
    private static final Logger logger = LoggerFactory.getLogger(FlowDailyReportController.class);
    @Resource
    private FlowDailyReportService flowDailyReportService;
    @Resource
    private FlowHistoryService flowHistoryService;
    @Resource
    private DeviceService deviceService;

    /**
     * 跳转到报表的实时监控列表页面方法
     *
     * @return
     */
    @RequestMapping(value = "/toRealTime")
    public ModelAndView toRealTime(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/reports/realTime");
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        modelAndView.addObject("sysuser", loginUser);
        return modelAndView;
    }

    /**
     * 跳转到时段报表页面方法
     *
     * @return
     */
    @RequestMapping(value = "/toTimeReports")
    public ModelAndView toTimeReports(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/reports/timeReports");
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        modelAndView.addObject("sysuser", loginUser);
        return modelAndView;
    }

    /**
     * 跳转到日报表页面方法
     *
     * @return
     */
    @RequestMapping(value = "/toDayReports")
    public ModelAndView toDayReports(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/reports/dayReports");
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        modelAndView.addObject("sysuser", loginUser);
        return modelAndView;
    }

    /**
     * 跳转到设备当月报表页面方法
     *
     * @return
     */
    @RequestMapping(value = "/toDeviceDayReports")
    public ModelAndView toDeviceDayReports(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/reports/daySecondaryReports");
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        modelAndView.addObject("sysuser", loginUser);
        return modelAndView;
    }

    /**
     * 跳转到月报表页面方法
     *
     * @return
     */
    @RequestMapping(value = "/toMonthReports")
    public ModelAndView toMonthReports(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/reports/monthReports");
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        modelAndView.addObject("sysuser", loginUser);
        return modelAndView;
    }

    /**
     * 跳转到水量年报表页面方法
     *
     * @return
     */
    @RequestMapping(value = "/toVolumeYearReports")
    public ModelAndView toVolumeYearReports(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/reports/waterVolumeReports");
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        modelAndView.addObject("sysuser", loginUser);
        return modelAndView;
    }

    /**
     * 跳转到水费年报表页面方法
     *
     * @return
     */
    @RequestMapping(value = "/toFeeYearReports")
    public ModelAndView toFeeYearReports(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/reports/waterFeeReports");
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        modelAndView.addObject("sysuser", loginUser);
        return modelAndView;
    }

    /**
     * 跳转到日报表图页面方法
     *
     * @return
     */
    @RequestMapping(value = "/toDayReportsChart")
    public ModelAndView toDayReportsChart(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/chart/dayReportsChart");
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        modelAndView.addObject("sysuser", loginUser);
        return modelAndView;
    }

    /**
     * 跳转到月报表图页面方法
     *
     * @return
     */
    @RequestMapping(value = "/toMonthReportsChart")
    public ModelAndView toMonthReportsChart(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/chart/monthReportsChart");
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        modelAndView.addObject("sysuser", loginUser);
        return modelAndView;
    }

    /**
     * 跳转到历史流量数据页面方法
     *
     * @return
     */
    @RequestMapping(value = "/toHistoryReports")
    public ModelAndView toHistoryReports(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/reports/history");
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        modelAndView.addObject("sysuser", loginUser);
        return modelAndView;
    }

    /**
     * 新增报表记录方法
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/insertFlowDailyReport", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertFlowDailyReport(FlowDailyReport flowDailyReport) throws Exception {
        JSONResult jsonResult = new JSONResult();
        flowDailyReportService.insert(flowDailyReport);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 删除报表记录方法
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/deleteFlowDailyReport", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteFlowDailyReport(FlowDailyReport flowDailyReport) throws Exception {
        JSONResult jsonResult = new JSONResult();
        flowDailyReportService.delete(flowDailyReport.getFlowDailyReportId().intValue());
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 修改报表记录方法
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/updateFlowDailyReport", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updateFlowDailyReport(FlowDailyReport flowDailyReport) throws Exception {
        JSONResult jsonResult = new JSONResult();
        flowDailyReportService.update(flowDailyReport);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询全部报表记录方法
     *
     * @param flowDailyReport
     * @return
     */
    @RequestMapping(value = "/selectFlowDailyReportList", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectFlowDailyReportList(FlowDailyReport flowDailyReport) {
        JSONResult jsonResult = new JSONResult();
        List<FlowDailyReport> list = flowDailyReportService.selectList(flowDailyReport);
        jsonResult.setData(list);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询报表记录详情方法
     *
     * @param flowDailyReport
     * @return
     */
    @RequestMapping(value = "/selectFlowDailyReportDetail", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectFlowDailyReportDetail(FlowDailyReport flowDailyReport) {
        JSONResult jsonResult = new JSONResult();
        FlowDailyReport flowDailyReportInfo = flowDailyReportService.selectOne(flowDailyReport);
        jsonResult.setData(flowDailyReportInfo);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 分页查询用户设备日报表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectFlowDailyReportPage", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectFlowDailyReportPage(HttpServletRequest request) {
        JSONResult jsonResult = new JSONResult();
        FlowDailyReport flowDailyReport = new FlowDailyReport();
        flowDailyReport.setStart(Integer.parseInt(request.getParameter("start")));
        flowDailyReport.setLength(Integer.parseInt(request.getParameter("length")));
        if (!StringUtils.isBlank(request.getParameter("createTime")))
            flowDailyReport.setDayTime(request.getParameter("createTime"));
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        flowDailyReport.setUserId(loginUser.getId());
        List<FlowDailyReport> flowDailyReportList = flowDailyReportService.selectPageList(flowDailyReport);
        int total = flowDailyReportService.queryTotal(flowDailyReport);
        int length = flowDailyReport.getLength();
        int start = flowDailyReport.getStart();
        PageUtils pager = new PageUtils(flowDailyReportList, total, length, start);
        jsonResult.setData(pager);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 分页查询某设备该月日报表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectDeviceDailyReportPage", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectDeviceDailyReportPage(HttpServletRequest request) {
        JSONResult jsonResult = new JSONResult();
        FlowDailyReport flowDailyReport = new FlowDailyReport();
        flowDailyReport.setStart(Integer.parseInt(request.getParameter("start")));
        flowDailyReport.setLength(Integer.parseInt(request.getParameter("length")));
        if (!StringUtils.isBlank(request.getParameter("deviceId")))
            flowDailyReport.setDeviceId(Integer.parseInt(request.getParameter("deviceId")));
        if (!StringUtils.isBlank(request.getParameter("createTime")))
            flowDailyReport.setDayTime(request.getParameter("createTime"));
        List<FlowDailyReport> flowDailyReportList = flowDailyReportService.selectDevicePageList(flowDailyReport);
        int total = flowDailyReportService.queryTotal(flowDailyReport);
        int length = flowDailyReport.getLength();
        int start = flowDailyReport.getStart();
        PageUtils pager = new PageUtils(flowDailyReportList, total, length, start);
        jsonResult.setData(pager);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 批量删除报表记录
     */
    @RequestMapping(value = "/deleteFlowDailyReports", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteFlowDailyReports(@RequestParam(value = "ids[]") Integer[] ids) {
        JSONResult jsonResult = new JSONResult();
        for (int i = 0; i < ids.length; i++) {
            flowDailyReportService.delete(ids[i]);
        }
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 全部设备日报表导出
     */
    @RequestMapping(value = "/excelDailyDownload")
    public void excelDailyDownload(HttpServletRequest request, HttpServletResponse response) {
        response.reset();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssms");
        SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(new Date());
        // 指定下载的文件名
        response.setHeader("Content-Disposition", "attachment;filename=" + dateStr + ".xlsx");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        //导出逻辑
        FlowDailyReport flowDailyReport = new FlowDailyReport();
        List<FlowDailyReport> creditInfoList = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("##0");
        if (!StringUtils.isBlank(request.getParameter("createTime"))) {
            flowDailyReport.setDayTime(request.getParameter("createTime"));
            Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
            flowDailyReport.setUserId(loginUser.getId());
            creditInfoList = flowDailyReportService.selectDailyExcel(flowDailyReport);
            if (creditInfoList != null && creditInfoList.size() > 0) {
                for (FlowDailyReport flowDailyReportInfo : creditInfoList) {
                    flowDailyReportInfo.setDailyReportCreateTime(sdformat.format(flowDailyReportInfo.getCreateTime()));
                    flowDailyReportInfo.setYesterdayReadingStr(df.format(flowDailyReportInfo.getYesterdayReadings()));
                    flowDailyReportInfo.setTodayReadingStr(df.format(flowDailyReportInfo.getTodayReadings()));
                    flowDailyReportInfo.setWaterAmountStr(df.format(flowDailyReportInfo.getWaterAmount()));
                }
            }
        }
        List<FlowDailyReport> ems = new ArrayList<>();
        Map<Integer, List<FlowDailyReport>> maps = new LinkedHashMap<>();
        SXSSFWorkbook book = null;
        ems.add(new FlowDailyReport("设备名称", "deviceName", 0));
        ems.add(new FlowDailyReport("上日读数", "yesterdayReadingStr", 0));
        ems.add(new FlowDailyReport("今日读数", "todayReadingStr", 0));
        ems.add(new FlowDailyReport("水量", "waterAmountStr", 0));
        ems.add(new FlowDailyReport("系数", "factor", 0));
        ems.add(new FlowDailyReport("单价", "price", 0));
        ems.add(new FlowDailyReport("时间", "dailyReportCreateTime", 0));
        ems.add(new FlowDailyReport("说明", "remark", 0));
        maps.put(0, ems);
        try {
            book = ExcelUtils.createExcelFile(FlowDailyReport.class, creditInfoList, maps, "流量数据日报表");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        OutputStream output;
        try {
            output = response.getOutputStream();

            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
            //刷新此缓冲的输出流，保证数据全部都能写出
            bufferedOutPut.flush();
            book.write(bufferedOutPut);
            bufferedOutPut.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 某设备某月日报表导出
     */
    @RequestMapping(value = "/excelDeviceDailyDownload")
    public void excelDeviceDailyDownload(HttpServletRequest request, HttpServletResponse response) {
        response.reset();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssms");
        SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(new Date());
        // 指定下载的文件名
        response.setHeader("Content-Disposition", "attachment;filename=" + dateStr + ".xlsx");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        //导出逻辑
        FlowDailyReport flowDailyReport = new FlowDailyReport();
        List<FlowDailyReport> creditInfoList = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("##0");
        if (!StringUtils.isBlank(request.getParameter("createTime")) && !StringUtils.isBlank(request.getParameter("deviceId"))) {
            flowDailyReport.setDayTime(request.getParameter("createTime"));
            flowDailyReport.setDeviceId(Integer.parseInt(request.getParameter("deviceId")));
            creditInfoList = flowDailyReportService.selectDeviceDailyExcel(flowDailyReport);
            if (creditInfoList != null && creditInfoList.size() > 0) {
                for (FlowDailyReport flowDailyReportInfo : creditInfoList) {
                    flowDailyReportInfo.setDailyReportCreateTime(sdformat.format(flowDailyReportInfo.getCreateTime()));
                    flowDailyReportInfo.setYesterdayReadingStr(df.format(flowDailyReportInfo.getYesterdayReadings()));
                    flowDailyReportInfo.setTodayReadingStr(df.format(flowDailyReportInfo.getTodayReadings()));
                    flowDailyReportInfo.setWaterAmountStr(df.format(flowDailyReportInfo.getWaterAmount()));
                }
            }
        }
        List<FlowDailyReport> ems = new ArrayList<>();
        Map<Integer, List<FlowDailyReport>> maps = new LinkedHashMap<>();
        SXSSFWorkbook book = null;
        ems.add(new FlowDailyReport("设备名称", "deviceName", 0));
        ems.add(new FlowDailyReport("上日读数", "yesterdayReadingStr", 0));
        ems.add(new FlowDailyReport("今日读数", "todayReadingStr", 0));
        ems.add(new FlowDailyReport("水量", "waterAmountStr", 0));
        ems.add(new FlowDailyReport("系数", "factor", 0));
        ems.add(new FlowDailyReport("单价", "price", 0));
        ems.add(new FlowDailyReport("时间", "dailyReportCreateTime", 0));
        ems.add(new FlowDailyReport("说明", "remark", 0));
        maps.put(0, ems);
        try {
            book = ExcelUtils.createExcelFile(FlowDailyReport.class, creditInfoList, maps, "流量数据日报表");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        OutputStream output;
        try {
            output = response.getOutputStream();

            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
            //刷新此缓冲的输出流，保证数据全部都能写出
            bufferedOutPut.flush();
            book.write(bufferedOutPut);
            bufferedOutPut.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 全部设备某月报表导出
     */
    @RequestMapping(value = "/excelMonthDownload")
    public void excelMonthDownload(HttpServletRequest request, HttpServletResponse response) {
        response.reset();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssms");
        String dateStr = sdf.format(new Date());
        // 指定下载的文件名
        response.setHeader("Content-Disposition", "attachment;filename=" + dateStr + ".xlsx");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        //导出逻辑
        FlowDailyReport flowDailyReport = new FlowDailyReport();
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        flowDailyReport.setUserId(loginUser.getId());
        List<FlowDailyReport> creditInfoList = flowDailyReportService.selectUserDeviceList(flowDailyReport);
        for (FlowDailyReport flowDailyReportInfo : creditInfoList) {
            flowDailyReport.setDayTime(request.getParameter("createTime") + "-01");
            flowDailyReport.setDeviceId(flowDailyReportInfo.getDeviceId());
            List<FlowDailyReport> list = flowDailyReportService.selectMonthExcelList(flowDailyReport);
            Device device = new Device();
            device.setDeviceId(flowDailyReportInfo.getDeviceId());
            device = deviceService.selectOne(device);
            //计算水费信息
            if (list != null && list.size() > 0) {
                flowDailyReportInfo.setDeviceName(device.getName());
                flowDailyReportInfo.setFactor(device.getFactor());
                flowDailyReportInfo.setPrice(device.getPrice());
                Float beginWaterAmount = list.get(0).getYesterdayReadings();
                Float endWaterAmount = list.get(list.size() - 1).getTodayReadings();
                Float waterAmount = endWaterAmount - beginWaterAmount;
                Float waterFee = waterAmount * device.getPrice();
                DecimalFormat df = new DecimalFormat("##0");
                flowDailyReportInfo.setBeginWaterAmount(df.format(beginWaterAmount));
                flowDailyReportInfo.setEndWaterAmount(df.format(endWaterAmount));
                flowDailyReportInfo.setWaterAmountStr(df.format(waterAmount));
                flowDailyReportInfo.setWaterFeeStr(df.format(waterFee));
            }
        }
        List<FlowDailyReport> ems = new ArrayList<>();
        Map<Integer, List<FlowDailyReport>> maps = new LinkedHashMap<>();
        SXSSFWorkbook book = null;
        ems.add(new FlowDailyReport("设备名称", "deviceName", 0));
        ems.add(new FlowDailyReport("始读数", "beginWaterAmount", 0));
        ems.add(new FlowDailyReport("终读数", "endWaterAmount", 0));
        ems.add(new FlowDailyReport("水量", "waterAmountStr", 0));
        ems.add(new FlowDailyReport("系数", "factor", 0));
        ems.add(new FlowDailyReport("单价", "price", 0));
        ems.add(new FlowDailyReport("水费", "waterFeeStr", 0));
        maps.put(0, ems);
        try {
            book = ExcelUtils.createExcelFile(FlowDailyReport.class, creditInfoList, maps, "流量数据月报表");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        OutputStream output;
        try {
            output = response.getOutputStream();

            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
            //刷新此缓冲的输出流，保证数据全部都能写出
            bufferedOutPut.flush();
            book.write(bufferedOutPut);
            bufferedOutPut.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 全部设备某年水量报表导出
     */
    @RequestMapping(value = "/excelYearWaterAmountDownload")
    public void excelYearWaterAmountDownload(HttpServletRequest request, HttpServletResponse response) {
        response.reset();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssms");
        String dateStr = sdf.format(new Date());
        DecimalFormat df = new DecimalFormat("##0");
        // 指定下载的文件名
        response.setHeader("Content-Disposition", "attachment;filename=" + dateStr + ".xlsx");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        //导出逻辑
        String year = request.getParameter("createTime");
        String[] month = {year + "-01-01", year + "-02-01", year + "-03-01", year + "-04-01", year + "-05-01", year + "-06-01", year + "-07-01", year + "-08-01", year + "-09-01", year + "-10-01", year + "-11-01", year + "-12-01"};
        FlowDailyReport flowDailyReport = new FlowDailyReport();
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        flowDailyReport.setUserId(loginUser.getId());
        List<FlowDailyReport> creditInfoList = flowDailyReportService.selectUserDeviceList(flowDailyReport);
        for (int i = 0; i < month.length; i++) {
            for (FlowDailyReport flowDailyReportInfo : creditInfoList) {
                //查询某个设备信息
                Device device = new Device();
                device.setDeviceId(flowDailyReportInfo.getDeviceId());
                device = deviceService.selectOne(device);
                //初始化赋值
                flowDailyReportInfo.setDeviceName(device.getName());
                flowDailyReportInfo.setPrice(device.getPrice());
                //查询某个设备该月全部日报表
                flowDailyReportInfo.setDeviceId(device.getDeviceId());
                flowDailyReportInfo.setDayTime(month[i]);
                List<FlowDailyReport> flowDeviceReportList = flowDailyReportService.selectDeviceMonthList(flowDailyReportInfo);
                //计算水费信息
                if (flowDeviceReportList != null && flowDeviceReportList.size() > 0) {
                    Float beginWaterAmount = flowDeviceReportList.get(0).getYesterdayReadings();
                    Float endWaterAmount = flowDeviceReportList.get(flowDeviceReportList.size() - 1).getTodayReadings();
                    Float waterAmount = endWaterAmount - beginWaterAmount;
                    String waterAmountStr = df.format(waterAmount);
                    String monthName = month[i].substring(5, 7);
                    switch (monthName) {
                        case "01":
                            flowDailyReportInfo.setJanuaryWaterAmount(waterAmountStr);
                            break;
                        case "02":
                            flowDailyReportInfo.setFebruaryWaterAmount(waterAmountStr);
                            break;
                        case "03":
                            flowDailyReportInfo.setMarchWaterAmount(waterAmountStr);
                            break;
                        case "04":
                            flowDailyReportInfo.setAprilWaterAmount(waterAmountStr);
                            break;
                        case "05":
                            flowDailyReportInfo.setMayWaterAmount(waterAmountStr);
                            break;
                        case "06":
                            flowDailyReportInfo.setJuneWaterAmount(waterAmountStr);
                            break;
                        case "07":
                            flowDailyReportInfo.setJulyWaterAmount(waterAmountStr);
                            break;
                        case "08":
                            flowDailyReportInfo.setAugustWaterAmount(waterAmountStr);
                            break;
                        case "09":
                            flowDailyReportInfo.setSeptemberWaterAmount(waterAmountStr);
                            break;
                        case "10":
                            flowDailyReportInfo.setOctoberWaterAmount(waterAmountStr);
                            break;
                        case "11":
                            flowDailyReportInfo.setNovemberWaterAmount(waterAmountStr);
                            break;
                        case "12":
                            flowDailyReportInfo.setDecemberWaterAmount(waterAmountStr);
                            break;
                    }
                }
            }
        }

        //水量合计
        for (FlowDailyReport flowDailyReportInfo : creditInfoList) {
            Float waterAmountTotal = 0f;
            if (flowDailyReportInfo.getJanuaryWaterAmount() != null)
                waterAmountTotal = waterAmountTotal + Float.parseFloat(flowDailyReportInfo.getJanuaryWaterAmount());
            if (flowDailyReportInfo.getFebruaryWaterAmount() != null)
                waterAmountTotal = waterAmountTotal + Float.parseFloat(flowDailyReportInfo.getFebruaryWaterAmount());
            if (flowDailyReportInfo.getMarchWaterAmount() != null)
                waterAmountTotal = waterAmountTotal + Float.parseFloat(flowDailyReportInfo.getMarchWaterAmount());
            if (flowDailyReportInfo.getAprilWaterAmount() != null)
                waterAmountTotal = waterAmountTotal + Float.parseFloat(flowDailyReportInfo.getAprilWaterAmount());
            if (flowDailyReportInfo.getMayWaterAmount() != null)
                waterAmountTotal = waterAmountTotal + Float.parseFloat(flowDailyReportInfo.getMayWaterAmount());
            if (flowDailyReportInfo.getJuneWaterAmount() != null)
                waterAmountTotal = waterAmountTotal + Float.parseFloat(flowDailyReportInfo.getJuneWaterAmount());
            if (flowDailyReportInfo.getJulyWaterAmount() != null)
                waterAmountTotal = waterAmountTotal + Float.parseFloat(flowDailyReportInfo.getJulyWaterAmount());
            if (flowDailyReportInfo.getAugustWaterAmount() != null)
                waterAmountTotal = waterAmountTotal + Float.parseFloat(flowDailyReportInfo.getAugustWaterAmount());
            if (flowDailyReportInfo.getSeptemberWaterAmount() != null)
                waterAmountTotal = waterAmountTotal + Float.parseFloat(flowDailyReportInfo.getSeptemberWaterAmount());
            if (flowDailyReportInfo.getOctoberWaterAmount() != null)
                waterAmountTotal = waterAmountTotal + Float.parseFloat(flowDailyReportInfo.getOctoberWaterAmount());
            if (flowDailyReportInfo.getNovemberWaterAmount() != null)
                waterAmountTotal = waterAmountTotal + Float.parseFloat(flowDailyReportInfo.getNovemberWaterAmount());
            if (flowDailyReportInfo.getDecemberWaterAmount() != null)
                waterAmountTotal = waterAmountTotal + Float.parseFloat(flowDailyReportInfo.getDecemberWaterAmount());
            flowDailyReportInfo.setWaterAmountTotalStr(df.format(waterAmountTotal));
        }

        List<FlowDailyReport> ems = new ArrayList<>();
        Map<Integer, List<FlowDailyReport>> maps = new LinkedHashMap<>();
        SXSSFWorkbook book = null;
        ems.add(new FlowDailyReport("设备名称", "deviceName", 0));
        ems.add(new FlowDailyReport("一月水量", "januaryWaterAmount", 0));
        ems.add(new FlowDailyReport("二月水量", "februaryWaterAmount", 0));
        ems.add(new FlowDailyReport("三月水量", "marchWaterAmount", 0));
        ems.add(new FlowDailyReport("四月水量", "aprilWaterAmount", 0));
        ems.add(new FlowDailyReport("五月水量", "mayWaterAmount", 0));
        ems.add(new FlowDailyReport("六月水量", "juneWaterAmount", 0));
        ems.add(new FlowDailyReport("七月水量", "julyWaterAmount", 0));
        ems.add(new FlowDailyReport("八月水量", "augustWaterAmount", 0));
        ems.add(new FlowDailyReport("九月水量", "septemberWaterAmount", 0));
        ems.add(new FlowDailyReport("十月水量", "octoberWaterAmount", 0));
        ems.add(new FlowDailyReport("十一月水量", "novemberWaterAmount", 0));
        ems.add(new FlowDailyReport("十二月水量", "decemberWaterAmount", 0));
        ems.add(new FlowDailyReport("合计", "waterAmountTotalStr", 0));
        maps.put(0, ems);
        try {
            book = ExcelUtils.createExcelFile(FlowDailyReport.class, creditInfoList, maps, "水量年报表");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        OutputStream output;
        try {
            output = response.getOutputStream();

            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
            //刷新此缓冲的输出流，保证数据全部都能写出
            bufferedOutPut.flush();
            book.write(bufferedOutPut);
            bufferedOutPut.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 全部设备某年水费报表导出
     */
    @RequestMapping(value = "/excelYearWaterFeeDownload")
    public void excelYearWaterFeeDownload(HttpServletRequest request, HttpServletResponse response) {
        response.reset();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssms");
        String dateStr = sdf.format(new Date());
        DecimalFormat df = new DecimalFormat("##0.00");
        // 指定下载的文件名
        response.setHeader("Content-Disposition", "attachment;filename=" + dateStr + ".xlsx");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        //导出逻辑
        String year = request.getParameter("createTime");
        String[] month = {year + "-01-01", year + "-02-01", year + "-03-01", year + "-04-01", year + "-05-01", year + "-06-01", year + "-07-01", year + "-08-01", year + "-09-01", year + "-10-01", year + "-11-01", year + "-12-01"};
        FlowDailyReport flowDailyReport = new FlowDailyReport();
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        flowDailyReport.setUserId(loginUser.getId());
        List<FlowDailyReport> creditInfoList = flowDailyReportService.selectUserDeviceList(flowDailyReport);
        for (int i = 0; i < month.length; i++) {
            for (FlowDailyReport flowDailyReportInfo : creditInfoList) {
                //查询某个设备信息
                Device device = new Device();
                device.setDeviceId(flowDailyReportInfo.getDeviceId());
                device = deviceService.selectOne(device);
                //初始化赋值
                flowDailyReportInfo.setDeviceName(device.getName());
                flowDailyReportInfo.setPrice(device.getPrice());
                //查询某个设备该月全部日报表
                flowDailyReportInfo.setDeviceId(device.getDeviceId());
                flowDailyReportInfo.setDayTime(month[i]);
                List<FlowDailyReport> flowDeviceReportList = flowDailyReportService.selectDeviceMonthList(flowDailyReportInfo);
                //计算水费信息
                if (flowDeviceReportList != null && flowDeviceReportList.size() > 0) {
                    Float beginWaterAmount = flowDeviceReportList.get(0).getYesterdayReadings();
                    Float endWaterAmount = flowDeviceReportList.get(flowDeviceReportList.size() - 1).getTodayReadings();
                    Float waterAmount = endWaterAmount - beginWaterAmount;
                    Float waterFee = waterAmount * device.getPrice();
                    String waterFeeStr = df.format(waterFee);
                    String monthName = month[i].substring(5, 7);
                    switch (monthName) {
                        case "01":
                            flowDailyReportInfo.setJanuaryWaterFee(waterFeeStr);
                            break;
                        case "02":
                            flowDailyReportInfo.setFebruaryWaterFee(waterFeeStr);
                            break;
                        case "03":
                            flowDailyReportInfo.setMarchWaterFee(waterFeeStr);
                            break;
                        case "04":
                            flowDailyReportInfo.setAprilWaterFee(waterFeeStr);
                            break;
                        case "05":
                            flowDailyReportInfo.setMayWaterFee(waterFeeStr);
                            break;
                        case "06":
                            flowDailyReportInfo.setJuneWaterFee(waterFeeStr);
                            break;
                        case "07":
                            flowDailyReportInfo.setJulyWaterFee(waterFeeStr);
                            break;
                        case "08":
                            flowDailyReportInfo.setAugustWaterFee(waterFeeStr);
                            break;
                        case "09":
                            flowDailyReportInfo.setSeptemberWaterFee(waterFeeStr);
                            break;
                        case "10":
                            flowDailyReportInfo.setOctoberWaterFee(waterFeeStr);
                            break;
                        case "11":
                            flowDailyReportInfo.setNovemberWaterFee(waterFeeStr);
                            break;
                        case "12":
                            flowDailyReportInfo.setDecemberWaterFee(waterFeeStr);
                            break;
                    }
                }
            }
        }

        //水费合计
        for (FlowDailyReport flowDailyReportInfo : creditInfoList) {
            Float waterFeeTotal = 0f;
            if (flowDailyReportInfo.getJanuaryWaterFee() != null)
                waterFeeTotal = waterFeeTotal + Float.parseFloat(flowDailyReportInfo.getJanuaryWaterFee());
            if (flowDailyReportInfo.getFebruaryWaterFee() != null)
                waterFeeTotal = waterFeeTotal + Float.parseFloat(flowDailyReportInfo.getFebruaryWaterFee());
            if (flowDailyReportInfo.getMarchWaterFee() != null)
                waterFeeTotal = waterFeeTotal + Float.parseFloat(flowDailyReportInfo.getMarchWaterFee());
            if (flowDailyReportInfo.getAprilWaterFee() != null)
                waterFeeTotal = waterFeeTotal + Float.parseFloat(flowDailyReportInfo.getAprilWaterFee());
            if (flowDailyReportInfo.getMayWaterFee() != null)
                waterFeeTotal = waterFeeTotal + Float.parseFloat(flowDailyReportInfo.getMayWaterFee());
            if (flowDailyReportInfo.getJuneWaterFee() != null)
                waterFeeTotal = waterFeeTotal + Float.parseFloat(flowDailyReportInfo.getJuneWaterFee());
            if (flowDailyReportInfo.getJulyWaterFee() != null)
                waterFeeTotal = waterFeeTotal + Float.parseFloat(flowDailyReportInfo.getJulyWaterFee());
            if (flowDailyReportInfo.getAugustWaterFee() != null)
                waterFeeTotal = waterFeeTotal + Float.parseFloat(flowDailyReportInfo.getAugustWaterFee());
            if (flowDailyReportInfo.getSeptemberWaterFee() != null)
                waterFeeTotal = waterFeeTotal + Float.parseFloat(flowDailyReportInfo.getSeptemberWaterFee());
            if (flowDailyReportInfo.getOctoberWaterFee() != null)
                waterFeeTotal = waterFeeTotal + Float.parseFloat(flowDailyReportInfo.getOctoberWaterFee());
            if (flowDailyReportInfo.getNovemberWaterFee() != null)
                waterFeeTotal = waterFeeTotal + Float.parseFloat(flowDailyReportInfo.getNovemberWaterFee());
            if (flowDailyReportInfo.getDecemberWaterFee() != null)
                waterFeeTotal = waterFeeTotal + Float.parseFloat(flowDailyReportInfo.getDecemberWaterFee());
            flowDailyReportInfo.setWaterFeeTotalStr(df.format(waterFeeTotal));
        }
        List<FlowDailyReport> ems = new ArrayList<>();
        Map<Integer, List<FlowDailyReport>> maps = new LinkedHashMap<>();
        SXSSFWorkbook book = null;
        ems.add(new FlowDailyReport("设备名称", "deviceName", 0));
        ems.add(new FlowDailyReport("一月水费", "januaryWaterFee", 0));
        ems.add(new FlowDailyReport("二月水费", "februaryWaterFee", 0));
        ems.add(new FlowDailyReport("三月水费", "marchWaterFee", 0));
        ems.add(new FlowDailyReport("四月水费", "aprilWaterFee", 0));
        ems.add(new FlowDailyReport("五月水费", "mayWaterFee", 0));
        ems.add(new FlowDailyReport("六月水费", "juneWaterFee", 0));
        ems.add(new FlowDailyReport("七月水费", "julyWaterFee", 0));
        ems.add(new FlowDailyReport("八月水费", "augustWaterFee", 0));
        ems.add(new FlowDailyReport("九月水费", "septemberWaterFee", 0));
        ems.add(new FlowDailyReport("十月水费", "octoberWaterFee", 0));
        ems.add(new FlowDailyReport("十一月水费", "novemberWaterFee", 0));
        ems.add(new FlowDailyReport("十二月水费", "decemberWaterFee", 0));
        ems.add(new FlowDailyReport("合计", "waterFeeTotalStr", 0));
        maps.put(0, ems);
        try {
            book = ExcelUtils.createExcelFile(FlowDailyReport.class, creditInfoList, maps, "水费年报表");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        OutputStream output;
        try {
            output = response.getOutputStream();

            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
            //刷新此缓冲的输出流，保证数据全部都能写出
            bufferedOutPut.flush();
            book.write(bufferedOutPut);
            bufferedOutPut.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 打印日报表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/printDailyReport")
    @ResponseBody
    public JSONResult printDailyReport(String content) {
        PrintUtils printUtils = new PrintUtils();
        printUtils.print(content);
        JSONResult jsonResult = new JSONResult();
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 分页查询全部设备月报表记录方法
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectFlowMonthReportPage", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectFlowMonthReportPage(HttpServletRequest request) {
        JSONResult jsonResult = new JSONResult();
        //分页查询全部用户设备
        Device deviceInfo =new Device();
        deviceInfo.setStart(Integer.parseInt(request.getParameter("start")));
        deviceInfo.setLength(Integer.parseInt(request.getParameter("length")));
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        deviceInfo.setUserId(loginUser.getId());
        List<Device> deviceList = deviceService.selectPageList(deviceInfo);
        //初始化报表
        List<FlowDailyReport> flowDailyReportList = new ArrayList<>();
        if (deviceList != null && deviceList.size() > 0) {
            for (Device device : deviceList) {
                //报表初始化赋值
                FlowDailyReport flowDailyReportInfo=new FlowDailyReport();
                flowDailyReportInfo.setDeviceName(device.getName());
                flowDailyReportInfo.setFactor(device.getFactor());
                flowDailyReportInfo.setPrice(device.getPrice());
                flowDailyReportInfo.setBeginWaterAmount("0.00");
                flowDailyReportInfo.setEndWaterAmount("0.00");
                flowDailyReportInfo.setWaterAmount(0f);
                flowDailyReportInfo.setWaterFee(0f);
                //查询某个设备该月全部日报表
                flowDailyReportInfo.setDeviceId(device.getDeviceId());
                flowDailyReportInfo.setDayTime(request.getParameter("createTime") + "-01");
                List<FlowDailyReport> flowDeviceReportList = flowDailyReportService.selectDeviceMonthList(flowDailyReportInfo);
                //计算水费信息
                if (flowDeviceReportList != null && flowDeviceReportList.size() > 0) {
                    Float beginWaterAmount = flowDeviceReportList.get(0).getYesterdayReadings();
                    Float endWaterAmount = flowDeviceReportList.get(flowDeviceReportList.size() - 1).getTodayReadings();
                    Float waterAmount = endWaterAmount - beginWaterAmount;
                    Float waterFee = waterAmount * device.getPrice();
                    DecimalFormat df = new DecimalFormat("##0");
                    flowDailyReportInfo.setBeginWaterAmount(df.format(beginWaterAmount));
                    flowDailyReportInfo.setEndWaterAmount(df.format(endWaterAmount));
                    flowDailyReportInfo.setWaterAmount(waterAmount);
                    flowDailyReportInfo.setWaterFee(waterFee);
                }
                flowDailyReportList.add(flowDailyReportInfo);
            }
        }
        //分页处理
        List<Device> totalList = deviceService.selectList(deviceInfo);
        int total = totalList.size();
        int length = deviceInfo.getLength();
        int start = deviceInfo.getStart();
        PageUtils pager = new PageUtils(flowDailyReportList, total, length, start);
        jsonResult.setData(pager);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 分页查询全部设备水量年报表记录方法
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectFlowAmountYearReportPage", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectFlowAmountYearReportPage(HttpServletRequest request) {
        JSONResult jsonResult = new JSONResult();
        String year = request.getParameter("createTime");
        String[] month = {year + "-01-01", year + "-02-01", year + "-03-01", year + "-04-01", year + "-05-01", year + "-06-01", year + "-07-01", year + "-08-01", year + "-09-01", year + "-10-01", year + "-11-01", year + "-12-01"};
        FlowDailyReport flowDailyReport = new FlowDailyReport();
        flowDailyReport.setStart(Integer.parseInt(request.getParameter("start")));
        flowDailyReport.setLength(Integer.parseInt(request.getParameter("length")));
        //分页查询全部设备
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        flowDailyReport.setUserId(loginUser.getId());
        List<FlowDailyReport> flowYearReportList = flowDailyReportService.selectUserDevicePage(flowDailyReport);
        for (int i = 0; i < month.length; i++) {
            if (flowYearReportList != null && flowYearReportList.size() > 0) {
                for (FlowDailyReport flowDailyReportInfo : flowYearReportList) {
                    //查询某个设备信息
                    Device device = new Device();
                    device.setDeviceId(flowDailyReportInfo.getDeviceId());
                    device = deviceService.selectOne(device);
                    //初始化赋值
                    flowDailyReportInfo.setDeviceName(device.getName());
                    flowDailyReportInfo.setPrice(device.getPrice());
                    //查询某个设备该月全部日报表
                    flowDailyReportInfo.setDeviceId(device.getDeviceId());
                    flowDailyReportInfo.setDayTime(month[i]);
                    List<FlowDailyReport> flowDeviceReportList = flowDailyReportService.selectDeviceMonthList(flowDailyReportInfo);
                    //计算水费信息
                    if (flowDeviceReportList != null && flowDeviceReportList.size() > 0) {
                        Float beginWaterAmount = flowDeviceReportList.get(0).getYesterdayReadings();
                        Float endWaterAmount = flowDeviceReportList.get(flowDeviceReportList.size() - 1).getTodayReadings();
                        Float waterAmount = 0f;
                        if (!beginWaterAmount.isNaN() && !endWaterAmount.isNaN()) {
                            waterAmount = endWaterAmount - beginWaterAmount;
                        }
                        DecimalFormat df = new DecimalFormat("##0");
                        String waterAmountStr = df.format(waterAmount);
                        String monthName = month[i].substring(5, 7);
                        switch (monthName) {
                            case "01":
                                flowDailyReportInfo.setJanuaryWaterAmount("0");
                                if (!StringUtils.isBlank(waterAmountStr))
                                    flowDailyReportInfo.setJanuaryWaterAmount(waterAmountStr);
                                Float waterAmountTotal = Float.parseFloat(flowDailyReportInfo.getJanuaryWaterAmount());
                                flowDailyReportInfo.setWaterAmountTotalStr(df.format(waterAmountTotal));
                                break;
                            case "02":
                                flowDailyReportInfo.setFebruaryWaterAmount("0");
                                if (!StringUtils.isBlank(waterAmountStr))
                                    flowDailyReportInfo.setFebruaryWaterAmount(waterAmountStr);
                                Float waterAmountTotal2 = Float.parseFloat(flowDailyReportInfo.getFebruaryWaterAmount());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterAmountTotalStr())) {
                                    waterAmountTotal2 = waterAmountTotal2 + Float.parseFloat(flowDailyReportInfo.getWaterAmountTotalStr());
                                }
                                flowDailyReportInfo.setWaterAmountTotalStr(df.format(waterAmountTotal2));
                                break;
                            case "03":
                                flowDailyReportInfo.setMarchWaterAmount("0");
                                if (!StringUtils.isBlank(waterAmountStr))
                                    flowDailyReportInfo.setMarchWaterAmount(waterAmountStr);
                                Float waterAmountTotal3 = Float.parseFloat(flowDailyReportInfo.getMarchWaterAmount());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterAmountTotalStr())) {
                                    waterAmountTotal3 = waterAmountTotal3 + Float.parseFloat(flowDailyReportInfo.getWaterAmountTotalStr());
                                }
                                flowDailyReportInfo.setWaterAmountTotalStr(df.format(waterAmountTotal3));
                                break;
                            case "04":
                                flowDailyReportInfo.setAprilWaterAmount("0");
                                if (!StringUtils.isBlank(waterAmountStr))
                                    flowDailyReportInfo.setAprilWaterAmount(waterAmountStr);
                                Float waterAmountTotal4 = Float.parseFloat(flowDailyReportInfo.getAprilWaterAmount());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterAmountTotalStr())) {
                                    waterAmountTotal4 = waterAmountTotal4 + Float.parseFloat(flowDailyReportInfo.getWaterAmountTotalStr());
                                }
                                flowDailyReportInfo.setWaterAmountTotalStr(df.format(waterAmountTotal4));
                                break;
                            case "05":
                                flowDailyReportInfo.setMayWaterAmount("0");
                                if (!StringUtils.isBlank(waterAmountStr))
                                    flowDailyReportInfo.setMayWaterAmount(waterAmountStr);
                                Float waterAmountTotal5 = Float.parseFloat(flowDailyReportInfo.getMayWaterAmount());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterAmountTotalStr())) {
                                    waterAmountTotal5 = waterAmountTotal5 + Float.parseFloat(flowDailyReportInfo.getWaterAmountTotalStr());
                                }
                                flowDailyReportInfo.setWaterAmountTotalStr(df.format(waterAmountTotal5));
                                break;
                            case "06":
                                flowDailyReportInfo.setJuneWaterAmount("0");
                                if (!StringUtils.isBlank(waterAmountStr))
                                    flowDailyReportInfo.setJuneWaterAmount(waterAmountStr);
                                Float waterAmountTotal6 = Float.parseFloat(flowDailyReportInfo.getJuneWaterAmount());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterAmountTotalStr())) {
                                    waterAmountTotal6 = waterAmountTotal6 + Float.parseFloat(flowDailyReportInfo.getWaterAmountTotalStr());
                                }
                                flowDailyReportInfo.setWaterAmountTotalStr(df.format(waterAmountTotal6));
                                break;
                            case "07":
                                flowDailyReportInfo.setJulyWaterAmount("0");
                                if (!StringUtils.isBlank(waterAmountStr))
                                    flowDailyReportInfo.setJulyWaterAmount(waterAmountStr);
                                Float waterAmountTotal7 = Float.parseFloat(flowDailyReportInfo.getJulyWaterAmount());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterAmountTotalStr())) {
                                    waterAmountTotal7 = waterAmountTotal7 + Float.parseFloat(flowDailyReportInfo.getWaterAmountTotalStr());
                                }
                                flowDailyReportInfo.setWaterAmountTotalStr(df.format(waterAmountTotal7));
                                break;
                            case "08":
                                flowDailyReportInfo.setAugustWaterAmount("0");
                                if (!StringUtils.isBlank(waterAmountStr))
                                    flowDailyReportInfo.setAugustWaterAmount(waterAmountStr);
                                Float waterAmountTotal8 = Float.parseFloat(flowDailyReportInfo.getAugustWaterAmount());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterAmountTotalStr())) {
                                    waterAmountTotal8 = waterAmountTotal8 + Float.parseFloat(flowDailyReportInfo.getWaterAmountTotalStr());
                                }
                                flowDailyReportInfo.setWaterAmountTotalStr(df.format(waterAmountTotal8));
                                break;
                            case "09":
                                flowDailyReportInfo.setSeptemberWaterAmount("0");
                                if (!StringUtils.isBlank(waterAmountStr))
                                    flowDailyReportInfo.setSeptemberWaterAmount(waterAmountStr);
                                Float waterAmountTotal9 = Float.parseFloat(flowDailyReportInfo.getSeptemberWaterAmount());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterAmountTotalStr())) {
                                    waterAmountTotal9 = waterAmountTotal9 + Float.parseFloat(flowDailyReportInfo.getWaterAmountTotalStr());
                                }
                                flowDailyReportInfo.setWaterAmountTotalStr(df.format(waterAmountTotal9));
                                break;
                            case "10":
                                flowDailyReportInfo.setOctoberWaterAmount("0");
                                if (!StringUtils.isBlank(waterAmountStr))
                                    flowDailyReportInfo.setOctoberWaterAmount(waterAmountStr);
                                Float waterAmountTotal10 = Float.parseFloat(flowDailyReportInfo.getSeptemberWaterAmount());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterAmountTotalStr())) {
                                    waterAmountTotal10 = waterAmountTotal10 + Float.parseFloat(flowDailyReportInfo.getWaterAmountTotalStr());
                                }
                                flowDailyReportInfo.setWaterAmountTotalStr(df.format(waterAmountTotal10));
                                break;
                            case "11":
                                flowDailyReportInfo.setNovemberWaterAmount("0");
                                if (!StringUtils.isBlank(waterAmountStr))
                                    flowDailyReportInfo.setNovemberWaterAmount(waterAmountStr);
                                Float waterAmountTotal11 = Float.parseFloat(flowDailyReportInfo.getNovemberWaterAmount());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterAmountTotalStr())) {
                                    waterAmountTotal11 = waterAmountTotal11 + Float.parseFloat(flowDailyReportInfo.getWaterAmountTotalStr());
                                }
                                flowDailyReportInfo.setWaterAmountTotalStr(df.format(waterAmountTotal11));
                                break;
                            case "12":
                                flowDailyReportInfo.setDecemberWaterAmount("0");
                                if (!StringUtils.isBlank(waterAmountStr))
                                    flowDailyReportInfo.setDecemberWaterAmount(waterAmountStr);
                                Float waterAmountTotal12 = Float.parseFloat(flowDailyReportInfo.getDecemberWaterAmount());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterAmountTotalStr())) {
                                    waterAmountTotal12 = waterAmountTotal12 + Float.parseFloat(flowDailyReportInfo.getWaterAmountTotalStr());
                                }
                                flowDailyReportInfo.setWaterAmountTotalStr(df.format(waterAmountTotal12));
                                break;
                        }
                    }
                }
            }
        }
        //分页处理
        List<FlowDailyReport> totalList = flowDailyReportService.selectUserDeviceList(flowDailyReport);
        int total = totalList.size();
        int length = flowDailyReport.getLength();
        int start = flowDailyReport.getStart();
        PageUtils pager = new PageUtils(flowYearReportList, total, length, start);
        jsonResult.setData(pager);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 分页查询全部设备水费年报表记录方法
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectFlowFeeYearReportPage", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectFlowFeeYearReportPage(HttpServletRequest request) {
        JSONResult jsonResult = new JSONResult();
        String year = request.getParameter("createTime");
        String[] month = {year + "-01-01", year + "-02-01", year + "-03-01", year + "-04-01", year + "-05-01", year + "-06-01", year + "-07-01", year + "-08-01", year + "-09-01", year + "-10-01", year + "-11-01", year + "-12-01"};
        FlowDailyReport flowDailyReport = new FlowDailyReport();
        flowDailyReport.setStart(Integer.parseInt(request.getParameter("start")));
        flowDailyReport.setLength(Integer.parseInt(request.getParameter("length")));
        //分页查询全部设备
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        flowDailyReport.setUserId(loginUser.getId());
        List<FlowDailyReport> flowYearReportList = flowDailyReportService.selectUserDevicePage(flowDailyReport);
        for (int i = 0; i < month.length; i++) {
            if (flowYearReportList != null && flowYearReportList.size() > 0) {
                for (FlowDailyReport flowDailyReportInfo : flowYearReportList) {
                    //查询某个设备信息
                    Device device = new Device();
                    device.setDeviceId(flowDailyReportInfo.getDeviceId());
                    device = deviceService.selectOne(device);
                    //初始化赋值
                    flowDailyReportInfo.setDeviceName(device.getName());
                    flowDailyReportInfo.setPrice(device.getPrice());
                    //查询某个设备该月全部日报表
                    flowDailyReportInfo.setDeviceId(device.getDeviceId());
                    flowDailyReportInfo.setDayTime(month[i]);
                    List<FlowDailyReport> flowDeviceReportList = flowDailyReportService.selectDeviceMonthList(flowDailyReportInfo);
                    //计算水费信息
                    if (flowDeviceReportList != null && flowDeviceReportList.size() > 0) {
                        Float beginWaterAmount = flowDeviceReportList.get(0).getYesterdayReadings();
                        Float endWaterAmount = flowDeviceReportList.get(flowDeviceReportList.size() - 1).getTodayReadings();
                        Float waterAmount = endWaterAmount - beginWaterAmount;
                        Float waterFee = waterAmount * device.getPrice();
                        DecimalFormat df2 = new DecimalFormat("##0.00");
                        String waterFeeStr = df2.format(waterFee);
                        String monthName = month[i].substring(5, 7);
                        switch (monthName) {
                            case "01":
                                flowDailyReportInfo.setJanuaryWaterFee("0.00");
                                if (!StringUtils.isBlank(waterFeeStr))
                                    flowDailyReportInfo.setJanuaryWaterFee(waterFeeStr);
                                Float waterFeeTotal = Float.parseFloat(flowDailyReportInfo.getJanuaryWaterFee());
                                flowDailyReportInfo.setWaterFeeTotalStr(df2.format(waterFeeTotal));
                                break;
                            case "02":
                                flowDailyReportInfo.setFebruaryWaterFee("0.00");
                                if (!StringUtils.isBlank(waterFeeStr))
                                    flowDailyReportInfo.setFebruaryWaterFee(waterFeeStr);
                                Float waterFeeTotal2 = Float.parseFloat(flowDailyReportInfo.getFebruaryWaterFee());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterFeeTotalStr())) {
                                    waterFeeTotal2 = waterFeeTotal2 + Float.parseFloat(flowDailyReportInfo.getWaterFeeTotalStr());
                                }
                                flowDailyReportInfo.setWaterFeeTotalStr(df2.format(waterFeeTotal2));
                                break;
                            case "03":
                                flowDailyReportInfo.setMarchWaterFee("0.00");
                                if (!StringUtils.isBlank(waterFeeStr))
                                    flowDailyReportInfo.setMarchWaterFee(waterFeeStr);
                                Float waterFeeTotal3 = Float.parseFloat(flowDailyReportInfo.getMarchWaterFee());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterFeeTotalStr())) {
                                    waterFeeTotal3 = waterFeeTotal3 + Float.parseFloat(flowDailyReportInfo.getWaterFeeTotalStr());
                                }
                                flowDailyReportInfo.setWaterFeeTotalStr(df2.format(waterFeeTotal3));
                                break;
                            case "04":
                                flowDailyReportInfo.setAprilWaterFee("0.00");
                                if (!StringUtils.isBlank(waterFeeStr))
                                    flowDailyReportInfo.setAprilWaterFee(waterFeeStr);
                                Float waterFeeTotal4 = Float.parseFloat(flowDailyReportInfo.getAprilWaterFee());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterFeeTotalStr())) {
                                    waterFeeTotal4 = waterFeeTotal4 + Float.parseFloat(flowDailyReportInfo.getWaterFeeTotalStr());
                                }
                                flowDailyReportInfo.setWaterFeeTotalStr(df2.format(waterFeeTotal4));
                                break;
                            case "05":
                                flowDailyReportInfo.setMayWaterFee("0.00");
                                if (!StringUtils.isBlank(waterFeeStr))
                                    flowDailyReportInfo.setMayWaterFee(waterFeeStr);
                                Float waterFeeTotal5 = Float.parseFloat(flowDailyReportInfo.getMayWaterFee());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterFeeTotalStr())) {
                                    waterFeeTotal5 = waterFeeTotal5 + Float.parseFloat(flowDailyReportInfo.getWaterFeeTotalStr());
                                }
                                flowDailyReportInfo.setWaterFeeTotalStr(df2.format(waterFeeTotal5));
                                break;
                            case "06":
                                flowDailyReportInfo.setJuneWaterFee("0.00");
                                if (!StringUtils.isBlank(waterFeeStr))
                                    flowDailyReportInfo.setJuneWaterFee(waterFeeStr);
                                Float waterFeeTotal6 = Float.parseFloat(flowDailyReportInfo.getJuneWaterFee());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterFeeTotalStr())) {
                                    waterFeeTotal6 = waterFeeTotal6 + Float.parseFloat(flowDailyReportInfo.getWaterFeeTotalStr());
                                }
                                flowDailyReportInfo.setWaterFeeTotalStr(df2.format(waterFeeTotal6));
                                break;
                            case "07":
                                flowDailyReportInfo.setJulyWaterFee("0.00");
                                if (!StringUtils.isBlank(waterFeeStr))
                                    flowDailyReportInfo.setJulyWaterFee(waterFeeStr);
                                Float waterFeeTotal7 = Float.parseFloat(flowDailyReportInfo.getJulyWaterFee());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterFeeTotalStr())) {
                                    waterFeeTotal7 = waterFeeTotal7 + Float.parseFloat(flowDailyReportInfo.getWaterFeeTotalStr());
                                }
                                flowDailyReportInfo.setWaterFeeTotalStr(df2.format(waterFeeTotal7));
                                break;
                            case "08":
                                flowDailyReportInfo.setAugustWaterFee("0.00");
                                if (!StringUtils.isBlank(waterFeeStr))
                                    flowDailyReportInfo.setAugustWaterFee(waterFeeStr);
                                Float waterFeeTotal8 = Float.parseFloat(flowDailyReportInfo.getAugustWaterFee());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterFeeTotalStr())) {
                                    waterFeeTotal8 = waterFeeTotal8 + Float.parseFloat(flowDailyReportInfo.getWaterFeeTotalStr());
                                }
                                flowDailyReportInfo.setWaterFeeTotalStr(df2.format(waterFeeTotal8));
                                break;
                            case "09":
                                flowDailyReportInfo.setSeptemberWaterFee("0.00");
                                if (!StringUtils.isBlank(waterFeeStr))
                                    flowDailyReportInfo.setSeptemberWaterFee(waterFeeStr);
                                Float waterFeeTotal9 = Float.parseFloat(flowDailyReportInfo.getSeptemberWaterFee());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterFeeTotalStr())) {
                                    waterFeeTotal9 = waterFeeTotal9 + Float.parseFloat(flowDailyReportInfo.getWaterFeeTotalStr());
                                }
                                flowDailyReportInfo.setWaterFeeTotalStr(df2.format(waterFeeTotal9));
                                break;
                            case "10":
                                flowDailyReportInfo.setOctoberWaterFee("0.00");
                                if (!StringUtils.isBlank(waterFeeStr))
                                    flowDailyReportInfo.setOctoberWaterFee(waterFeeStr);
                                Float waterFeeTotal10 = Float.parseFloat(flowDailyReportInfo.getOctoberWaterFee());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterFeeTotalStr())) {
                                    waterFeeTotal10 = waterFeeTotal10 + Float.parseFloat(flowDailyReportInfo.getWaterFeeTotalStr());
                                }
                                flowDailyReportInfo.setWaterFeeTotalStr(df2.format(waterFeeTotal10));
                                break;
                            case "11":
                                flowDailyReportInfo.setNovemberWaterFee("0.00");
                                if (!StringUtils.isBlank(waterFeeStr))
                                    flowDailyReportInfo.setNovemberWaterFee(waterFeeStr);
                                Float waterFeeTotal11 = Float.parseFloat(flowDailyReportInfo.getNovemberWaterFee());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterFeeTotalStr())) {
                                    waterFeeTotal11 = waterFeeTotal11 + Float.parseFloat(flowDailyReportInfo.getWaterFeeTotalStr());
                                }
                                flowDailyReportInfo.setWaterFeeTotalStr(df2.format(waterFeeTotal11));
                                break;
                            case "12":
                                flowDailyReportInfo.setDecemberWaterFee("0.00");
                                if (!StringUtils.isBlank(waterFeeStr))
                                    flowDailyReportInfo.setDecemberWaterFee(waterFeeStr);
                                Float waterFeeTotal12 = Float.parseFloat(flowDailyReportInfo.getDecemberWaterFee());
                                if (!StringUtils.isBlank(flowDailyReportInfo.getWaterFeeTotalStr())) {
                                    waterFeeTotal12 = waterFeeTotal12 + Float.parseFloat(flowDailyReportInfo.getWaterFeeTotalStr());
                                }
                                flowDailyReportInfo.setWaterFeeTotalStr(df2.format(waterFeeTotal12));
                                break;
                        }
                    }
                }
            }
        }
        //分页处理
        List<FlowDailyReport> totalList = flowDailyReportService.selectUserDeviceList(flowDailyReport);
        int total = totalList.size();
        int length = flowDailyReport.getLength();
        int start = flowDailyReport.getStart();
        PageUtils pager = new PageUtils(flowYearReportList, total, length, start);
        jsonResult.setData(pager);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询日报表图方法
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectFlowDailyReportChart", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectFlowDailyReportChart(HttpServletRequest request) {
        JSONResult jsonResult = new JSONResult();
        FlowDailyReport flowDailyReport = new FlowDailyReport();
        if (!StringUtils.isBlank(request.getParameter("deviceId")))
            flowDailyReport.setDeviceId(Integer.parseInt(request.getParameter("deviceId")));
        if (!StringUtils.isBlank(request.getParameter("createTime")))
            flowDailyReport.setDayTime(request.getParameter("createTime") + "-01");
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        flowDailyReport.setUserId(loginUser.getId());
        List<FlowDailyReport> list = flowDailyReportService.selectFlowDailyReportChart(flowDailyReport);
        jsonResult.setData(list);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询月报表图方法
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectFlowMonthReportChart", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectFlowMonthReportChart(HttpServletRequest request) {
        JSONResult jsonResult = new JSONResult();
        FlowDailyReport flowDailyReport = new FlowDailyReport();
        if (!StringUtils.isBlank(request.getParameter("deviceId")))
            flowDailyReport.setDeviceId(Integer.parseInt(request.getParameter("deviceId")));
        if (!StringUtils.isBlank(request.getParameter("createTime")))
            flowDailyReport.setDayTime(request.getParameter("createTime") + "-01-01");
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        flowDailyReport.setUserId(loginUser.getId());
        List<FlowDailyReport> list = flowDailyReportService.selectFlowMonthReportChart(flowDailyReport);
        jsonResult.setData(list);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 分页查询全部设备时段报表记录方法
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectFlowSecondReportPage", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectFlowSecondReportPage(HttpServletRequest request) {
        logger.debug("查询时段报表开始：");
        JSONResult jsonResult = new JSONResult();
        Device deviceInfo =new Device();
        deviceInfo.setStart(Integer.parseInt(request.getParameter("start")));
        deviceInfo.setLength(Integer.parseInt(request.getParameter("length")));
        //分页查询全部用户设备
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        deviceInfo.setUserId(loginUser.getId());
        List<Device> deviceList = deviceService.selectPageList(deviceInfo);
        List<FlowDailyReport> flowDailyReportList = new ArrayList<>();
        for (Device device : deviceList) {
            //初始化赋值
            FlowDailyReport flowDailyReportInfo=new FlowDailyReport();
            flowDailyReportInfo.setDeviceName(device.getName());
            flowDailyReportInfo.setFactor(device.getFactor());
            flowDailyReportInfo.setPrice(device.getPrice());
            flowDailyReportInfo.setBeginWaterAmount("0.00");
            flowDailyReportInfo.setEndWaterAmount("0.00");
            flowDailyReportInfo.setWaterAmount(0f);
            //查询某个设备时段报表
            FlowHistory flowHistory = new FlowHistory();
            flowHistory.setDeviceId(device.getDeviceId());
            flowHistory.setStartTime(request.getParameter("startTime"));
            flowHistory.setEndTime(request.getParameter("endTime"));
            logger.debug("查询历史流量数据开始：");
            List<FlowHistory> flowSecondList = flowHistoryService.selectSecondList(flowHistory);
            logger.debug("查询历史流量数据结束：");
            //计算水量信息
            if (flowSecondList != null && flowSecondList.size() > 0) {
                Float beginWaterAmount = flowSecondList.get(0).getTotalPositive();
                Float endWaterAmount = flowSecondList.get(flowSecondList.size() - 1).getTotalPositive();
                Float waterAmount = endWaterAmount - beginWaterAmount;
                DecimalFormat df = new DecimalFormat("##0");
                flowDailyReportInfo.setBeginWaterAmount(df.format(beginWaterAmount));
                flowDailyReportInfo.setEndWaterAmount(df.format(endWaterAmount));
                flowDailyReportInfo.setWaterAmount(waterAmount);
            }
            flowDailyReportList.add(flowDailyReportInfo);
        }
        //分页处理
        List<Device> totalList = deviceService.selectList(deviceInfo);
        int total = totalList.size();
        int length = deviceInfo.getLength();
        int start = deviceInfo.getStart();
        PageUtils pager = new PageUtils(flowDailyReportList, total, length, start);
        jsonResult.setData(pager);
        jsonResult.setSuccess(true);
        logger.debug("查询时段报表结束：");
        return jsonResult;
    }

    /**
     * 查询设备某月水量总和
     *
     * @param flowDailyReport
     * @return
     */
    @RequestMapping(value = "/queryDeviceFlowTotal", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult queryDeviceFlowTotal(FlowDailyReport flowDailyReport) {
        JSONResult jsonResult = new JSONResult();
        int waterAmount = flowDailyReportService.queryDeviceFlowTotal(flowDailyReport);
        jsonResult.setData(waterAmount);
        jsonResult.setSuccess(true);
        return jsonResult;
    }
}
